import { ElLoading } from 'element-plus'
import { Base64 } from 'js-base64'
let showloading = null
export function showLoading() {
  if (showloading == null) {
    showloading = ElLoading.service({
      lock: true,
      text: '加载中...',
      background: 'rgba(255, 255, 255, 0.8)',
    })
  }
}
export function hideLoding() {
  showloading.close()
}
export function imgBase(val) {
  let text = JSON.parse(JSON.stringify(val)) //深拷贝
  let index = text.indexOf('<img') //找到第一个img
  while (index !== -1) {
    //循环执行替换方法
    let imgText = text.substring(index, text.indexOf('>', index + 1) + 1) //拿到img
    if (imgText.indexOf('mathTypeImage') !== -1) {
      //判断img是不是可转换对象
      if (imgText.indexOf("attribute=''") == -1 && imgText.indexOf('attribute=""') == -1) {
        let attributeIndex = imgText.indexOf('attribute="') + 11 //获取attribute
        text = text.replace(
          //将img替换成Base64转换后的值
          imgText,
          Base64.decode(
            imgText.substring(
              attributeIndex,
              imgText.indexOf('"', attributeIndex + 1)
            )
          )
        )
      }
    }
    index = text.indexOf('<img', index + 1)
  }
  text = text.replace(/mml:/g, '')
  return text
}
export function $$Base(val){
  val = val.replace(/\$\$/, "\\(");
  val = val.replace(/\$\$/, "\\)");
  return val
}
export function JsonBase(val) {
  val.subjectEditorValue ? val.subjectEditorValue = imgBase(val.subjectEditorValue) : ''
  val.explainEditorValue ? val.explainEditorValue = imgBase(val.explainEditorValue) : ''
  val.answerEditorValue ? val.answerEditorValue = imgBase(val.answerEditorValue) : ''
  val.subjectEditorValue ? val.subjectEditorValue = $$Base(val.subjectEditorValue) : ''
  val.explainEditorValue ? val.explainEditorValue = $$Base(val.explainEditorValue) : ''
  val.answerEditorValue ? val.answerEditorValue = $$Base(val.answerEditorValue) : ''
  if (['dan_xuan', 'duo_xuan', 'pan_duan', 'any_xuan', 'tian_kong'].includes(val.questionTypeBase)) {
    val.options.forEach(res => {
      res.optionTextValue ? res.optionTextValue = imgBase(res.optionTextValue) : ''
      res.optionTipEditorValue ? res.optionTipEditorValue = imgBase(res.optionTipEditorValue) : ''
      res.optionTextValue ? res.optionTextValue = $$Base(res.optionTextValue) : ''
      res.optionTipEditorValue ? res.optionTipEditorValue = $$Base(res.optionTipEditorValue) : ''
    })
  }
  if (['zong_he'].includes(val.questionTypeBase)) {
    val.subQuestionList.forEach(res => {
      res = JsonBase(res)
    })
  }
  return val
}
export function transition(questiontypeList, val) {
  //题型拼音转中文
  let text = ''
  if (questiontypeList) {
    questiontypeList.forEach(item => {
      if (item.code == val) {
        text = item.name
      }
    });
  } else {
    switch (val) {
      case 'dan_xuan':
        text = '单选'
        break;
      case 'duo_xuan':
        text = '多选'
        break;
      case 'pan_duan':
        text = '判断'
        break;
      case 'tian_kong':
        text = '填空'
        break;
      case 'wen_da':
        text = '问答'
        break;
      case 'ting_li':
        text = '听力'
        break;
      case 'zong_he':
        text = '综合'
        break;
      case 'any_xuan':
        text = '不定项选择'
        break;
      case 'pai_xu':
        text = '排序'
        break;
      default:
        text = '判断'
        break;
    }
  }

  return questiontypeList ? text : text + '题'
}
export function deep(val) {
  return JSON.parse(JSON.stringify(val))
}
export function uuid32() {
  var s = []
  var hexDigits = '0123456789abcdefghigk'
  for (var i = 0; i < 32; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  var uuid = s.join('')
  return uuid
}
export function changeNum(num) {
  var arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  var arr2 = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']
  if (!num || isNaN(num)) return '零'
  var english = num.toString().split('')
  var result = ''
  for (var i = 0; i < english.length; i++) {
    var des_i = english.length - 1 - i// 倒序排列设值
    result = arr2[i] + result
    var arr1_index = english[des_i]
    result = arr1[arr1_index] + result
  }
  result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十') // 将【零千、零百】换成【零】 【十零】换成【十】
  result = result.replace(/零+/g, '零') // 合并中间多个零为一个零
  result = result.replace(/零亿/g, '亿').replace(/零万/g, '万') // 将【零亿】换成【亿】【零万】换成【万】
  result = result.replace(/亿万/g, '亿') // 将【亿万】换成【亿】
  result = result.replace(/零+$/, '') // 移除末尾的零
  // 将【一十】换成【十】
  result = result.replace(/^一十/g, '十')
  return result
}
export function getNowFormatDate() {
  let date = new Date(),
    seperator1 = '-', //格式分隔符
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
  if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数，在前面补0
  if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数，在前面补0

  let currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate
}